import { defineStore } from "pinia";
import { ref, UnwrapNestedRefs } from "vue";
import { ConsultGrade, ConsultType } from "@/enum";
import { consultOrderPayAPI, createConsultOrderAPI, fetchConsultOrderDetailAPI, fetchDepartmentListAPI, fetchOrderPreAPI, fetchPrescriptionPicAPI, uploadAPI } from "@/api/consultApi.ts";
import type { Department, PartialConsult, ConsultIllness, ConsultOrderPreParams, ConsultOrderPreData, PayParams } from "@/types/consultTypes";
import type { ConsultOrderItem } from "@/types/chitchat";

const useConsultStore = defineStore("consultStore", () => {
    /*问诊信息*/
    const consultInfo = ref<PartialConsult>(<PartialConsult>{});
    /*科室列表*/
    const departmentList = ref<Department[]>([]);
    /*订单预支付信息*/
    const orderPre = ref<ConsultOrderPreData>(<ConsultOrderPreData>{});
    /*订单详情*/
    const orderDetail = ref<ConsultOrderItem>(<ConsultOrderItem>{});
    /*第一步：记录问诊类型的函数---问医生1、极速问诊2、开药门诊3*/
    const setType = (consultType: ConsultType): void => {
        consultInfo.value.type = consultType;
    };

    /*第二步：记录问诊的级别---三甲问诊还是图文问诊*/
    const setIllnessType = (grade: ConsultGrade): void => {
        consultInfo.value.illnessType = grade;
    };

    /*第三步：记录问诊的科室ID*/
    const setDepartmentId = (departmentId: string): void => {
        consultInfo.value.depId = departmentId;
    };

    /*第四步：记录患者的病情描述*/
    const setIllnessInfo = (illnessInfo: ConsultIllness): void => {
        consultInfo.value.illnessDesc = illnessInfo.illnessDesc;
        consultInfo.value.pictures = illnessInfo.pictures;
        consultInfo.value.consultFlag = illnessInfo.consultFlag;
        consultInfo.value.illnessTime = illnessInfo.illnessTime;
    };
    /*第五步:记录患者的ID*/
    const setPatientId = (patientId: string): void => {
        consultInfo.value.patientId = patientId;
    };
    /*设置优惠券的函数*/
    const setCouponId = (id: string): void => {
        consultInfo.value.couponId = id;
    };

    const clearConsultInfo = (): void => {
        consultInfo.value = {};
    };
    /*获取科室列表的请求函数*/
    const fetchDepartmentList = async (): Promise<void> => {
        const { data }: { data: Department[] } = await fetchDepartmentListAPI();
        departmentList.value = data;
    };

    /*文件上传的请求函数*/
    const upload = async (formData: FormData): Promise<{ id: string, url: string }> => {
        const { data }: { data: { id: string, url: string } } = await uploadAPI(formData);
        return data;
    };

    /*获取订单预支付的请求函数*/
    const fetchOrderPre = async (params: UnwrapNestedRefs<ConsultOrderPreParams>): Promise<void> => {
        const { data }: { data: ConsultOrderPreData } = await fetchOrderPreAPI(params);
        orderPre.value = data;
        // 设置优惠券
        setCouponId(data.couponId);
    };

    /*创建订单的请求函数*/
    const createConsultOrder = async (consultInfo: PartialConsult): Promise<string> => {
        const { data }: { data: { id: string } } = await createConsultOrderAPI(consultInfo);
        return data.id;
    };

    /*获取订单详情的请求函数*/
    const fetchConsultOrderDetail = async (orderId: string): Promise<void> => {
        const { data }: { data: ConsultOrderItem } = await fetchConsultOrderDetailAPI(orderId);
        orderDetail.value = data;
    };

    /*支付的请求函数*/
    const consultOrderPay = async (payParams: PayParams): Promise<string> => {
        const { data }: { data: { payUrl: string } } = await consultOrderPayAPI(payParams);
        return data.payUrl;
    };

    /*查看处方的请求函数*/
    const fetchPrescriptionPic = async (id: string): Promise<string> => {
        const { data }: { data: { url: string } } = await fetchPrescriptionPicAPI(id);
        return data.url;
    };
    return {
        consultInfo,
        departmentList,
        orderPre,
        orderDetail,
        upload,
        setType,
        setIllnessType,
        setDepartmentId,
        setIllnessInfo,
        setPatientId,
        setCouponId,
        clearConsultInfo,
        fetchDepartmentList,
        fetchOrderPre,
        createConsultOrder,
        consultOrderPay,
        fetchConsultOrderDetail,
        fetchPrescriptionPic
    };
}, {
    persist: {
        paths: ["consultInfo"],
    },
});


export default useConsultStore;
